<?php
namespace app\shop\model;

use app\BaseModel;
use app\farm\model\ShopGoods;
use think\facade\Db;

class ShopGoodsCate extends BaseModel
{
    //定义表名
    protected $name = 'lbfarm_shop_goods_cate';



    protected $append = [

        'goods_num',

        'store'

    ];


    /**
     * @param $value
     * @param $data
     * @功能说明:
     * @author chenniang
     * @DataTime: 2022-07-04 17:23
     */
    public function getStoreAttr($value,$data){

        if(!empty($data['id'])){

            $dis = [

                'b.status' => 2,

                'a.goods_id' => $data['id'],

                'a.type'     => 2
            ];

            $store_goods_model = new StoreGoods();

            $list = $store_goods_model->alias('a')
                    ->join('lbfarm_farmer b','a.store_id = b.id')
                    ->where($dis)
                    ->field('a.*,b.title')
                    ->group('a.id')
                    ->order('a.id dsec')
                    ->column('b.id');

            return array_values($list);
        }


    }

    /**
     * @author chenniang
     * @DataTime: 2021-03-23 13:46
     * @功能说明:分类下面端商品数量
     */
    public function getGoodsNumAttr($value,$data){

        if(!empty($data['id'])){

            $goods_model = new ShopGoods();

            $num = $goods_model->where(['cate_id'=>$data['id']])->where('status','>',-1)->count();

            return $num;
        }

    }

    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();

        if(!empty($data['store'])){

            $store = $data['store'];

            unset($data['store']);
        }

        $res = $this->insert($data);

        $id = $this->getLastInsID();

        if(isset($store)){

            $this->updateSome($id,$store,$data['uniacid']);
        }

        return $res;

    }


    /**
     * @param $id
     * @param $data
     * @param $uniacid
     * @功能说明:
     * @author chenniang
     * @DataTime: 2022-07-04 14:47
     */
    public function updateSome($id,$data,$uniacid){

        $store_goods_model = new StoreGoods();

        $store_goods_model->where(['goods_id'=>$id,'type'=>2])->delete();

        if(!empty($data)){

            foreach ($data as $k=>$v){

                $insert[$k] = [

                    'uniacid' => $uniacid,

                    'store_id'=> $v,

                    'goods_id'=> $id,

                    'type'    => 2
                ];

            }

            $store_goods_model->saveAll($insert);

        }

        return true;

    }

    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        if(!empty($data['store'])){

            $store = $data['store'];

            unset($data['store']);
        }

        $res = $this->where($dis)->update($data);

        if(isset($store)){

            $this->updateSome($dis['id'],$store,$data['uniacid']);
        }

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page){

        $data = $this->where($dis)->order('top desc,id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }







}